package cn.dglydrpy.study.ssm.common.rabbitmq.annotation;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

import java.io.UnsupportedEncodingException;

public class RabbitAnnotationConsumer {
    public static void main(String[] args) throws UnsupportedEncodingException {

        // 从指定类加载配置信息
        AbstractApplicationContext context = new AnnotationConfigApplicationContext(RabbitConsumerConfig.class);
        // 获取RabbitTemplate对象
        final RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
        
        while (true) {
        	// 接收消息
            final Message message = rabbitTemplate.receive("queue.ssm.rabbit.ano");
       	    if(message != null) {
           	 // 拉消息模式
       	     // 打印消息
       	        System.out.println("RabbitAnnotationConsumer接收消息： " + new String(message.getBody(), message.getMessageProperties().getContentEncoding()));
//              System.out.println(new String(message.getBody(), "utf-8"));
           }else {
           	break;
           }
       }

        // 关闭spring的上下文
//        context.close();

    }
}
